package com.roin.learn.activiti.controller;

import com.roin.learn.activiti.base.BaseController;
import com.roin.learn.activiti.base.SysConstent;
import com.roin.learn.activiti.entity.Page;
import com.roin.learn.activiti.entity.PersonalTask;
import com.roin.learn.activiti.entity.ResourceResult;
import com.roin.learn.activiti.entity.User;
import com.roin.learn.activiti.exception.BusinessException;
import com.roin.learn.activiti.service.ProcessService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人任务Controller
 */
@RequestMapping("personal")
@Controller
public class PersonalTaskController extends BaseController{

    @Resource
    ProcessService processService;

    /**
     * 跳转到个人任务页面
     * @return
     */
    @RequestMapping("task.htm")
    public String toPersonalTask(){
        return "personal/task";
    }


    /**
     * 分页查询个人任务
     * @param pageNum
     * @param pageSize
     * @param session
     * @return
     */
    @RequestMapping("task/list.json")
    @ResponseBody
    public Page getPage(Integer pageNum, Integer pageSize, HttpSession session) {
        User user = checkLogin(session);
        Page<PersonalTask> page = new Page<>();
        try {
            Page<Task> pageTemp = processService.queryAssigneeTask(user.getUserId(),pageNum,pageSize);
            List<PersonalTask> tasks = new ArrayList<>();
            for (Object o : pageTemp.getList()) {
                PersonalTask task = new PersonalTask();
                BeanUtils.copyProperties(o,task);
                tasks.add(task);
            }
            page.setList(tasks);
            page.setTotal(pageTemp.getTotal());
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
        } catch (Exception e) {
            log.error("分页查询用户任务异常",e);
            page.setMsg(SysConstent.MSG_SYS_ERROR);
            page.setStatus(500);
        }
        return page;
    }

    /**
     * 跳转到个人可认领（签收）任务面
     * @return
     */
    @RequestMapping("candidateTask.htm")
    public String toCandidateTask(){
        return "personal/candidateTask";
    }
    /**
     * 分页查询个人可认领（签收）任务
     * @param pageNum
     * @param pageSize
     * @param session
     * @return
     */
    @RequestMapping("candidateTask/list.json")
    @ResponseBody
    public Page getCandidateUserTaskPage(Integer pageNum, Integer pageSize, HttpSession session) {
        User user = checkLogin(session);
        Page<PersonalTask> page = new Page<>();
        try {
            Page<Task> pageTemp = processService.queryCandidateUserTask(user.getUserId(),pageNum,pageSize);
            List<PersonalTask> tasks = new ArrayList<>();
            for (Object o : pageTemp.getList()) {
                PersonalTask task = new PersonalTask();
                BeanUtils.copyProperties(o,task);
                tasks.add(task);
            }
            page.setList(tasks);
            page.setTotal(pageTemp.getTotal());
            page.setPageNum(pageNum);
            page.setPageSize(pageSize);
        } catch (Exception e) {
            log.error("分页查询用户可认领（签收）任务异常",e);
            page.setMsg(SysConstent.MSG_SYS_ERROR);
            page.setStatus(500);
        }
        return page;
    }

    /**
     * 认领（签收）任务
     * @param taskId
     * @return
     */
    @RequestMapping("task/claim.do")
    @ResponseBody
    public ResourceResult claimTask(String taskId,HttpSession session){
        ResourceResult result=new ResourceResult();
        try {
            User user = checkLogin(session);
            processService.takeTask(taskId,user.getUserId());
            result.setSuccess(true).setMessage("签收成功！");
        } catch (BusinessException e) {
            result.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("用户认领（签收）任务异常",e);
            result.setMessage(SysConstent.MSG_SYS_ERROR);
        }
        return result;
    }
}
